var foodDao = require('../../dao/foodDao.js');
const winston = require("../../utils/winstonLogger.js");

module.exports = {
    getFoodListByMerchantId : getFoodListByMerchantId,
    addFood : addFood,
    updateFood : updateFood,
    deleteFood : deleteFood,
    getFoodListByType : getFoodListByType,
    getFoodsByTypeAndMerchantId: getFoodsByTypeAndMerchantId
}


function getFoodsByTypeAndMerchantId(req, res, next){
    let type = req.params.type;
    let merchantId = req.params.merchantId;
    if(type == 1) {type = "酒水"}
    else if(type == 2) {type = "套餐"}
    else if(type == 3) {type = "主食"}
    else if(type == 4) {type = "小吃"}
    winston.info(type+"____"+merchantId);
    foodDao.queryFood({type:type, merchant:merchantId}, (err, result)=>{
        if(err){
            winston.error("query error......");
        }else{
            res.json(result);
        }
    });
}

function getFoodListByMerchantId(req,res,next) {
    let merchantId = req.params.merchantId;
    foodDao.queryFood({merchant:merchantId},(err,result)=>{
        if(err){
            console.log("query food error !");
        }else{
            res.json(result);
        }
    })
}

function getFoodListByType(req, res, next){
    let type = req.params.type;
    foodDao.queryFood({type:type}, (err, result) => {
        if(err){
            winston.info("query food by type error !")
        }else{
            res.json(result);
        }
    })
}

function addFood(req,res,next) {
    let food = req.body;
    foodDao.addFood(food,(err,result)=>{
        if(err){
            console.log("add food error !");
        }else{
            res.json(result);
        }
    })
}

function updateFood(req,res,next) {
    let newFood = req.body;
    foodDao.updateFood(newFood,(err,result)=>{
        if(err){
            console.log("update food error !");
        }else{
            res.json(result);
        }
    })
}

function deleteFood(req,res,next) {
    let foodId = req.params.foodId;
    foodDao.deleteFood(foodId,(err,result)=>{
        if(err){
            console.log("delete food error !");
        }else{
            res.json(result);
        }
    })
}